package com.tanhua.server.service;


import com.tanhua.commons.templates.HuanXinTemplate;
import com.tanhua.domain.db.Announcement;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.mongo.UserLike;
import com.tanhua.domain.vo.AnnouncementVo;
import com.tanhua.domain.vo.ContactVo;

import com.tanhua.domain.vo.MessageVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.AnnouncementApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.mongo.FriendApi;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.utils.RelativeDateFormat;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.w3c.dom.css.CSSPageRule;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AnnounService {
    @Reference
    private AnnouncementApi announcementApi;
    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @Reference
    private FriendApi friendApi;
    @Reference
    private UserInfoApi userInfoApi;

    public PageResult<AnnouncementVo> announcement(Long page, Long pageSize) {
        PageResult pageResult = announcementApi.findPage(page, pageSize);
        List<Announcement> items = pageResult.getItems();
        List<AnnouncementVo> announlist = new ArrayList<>();
        for (Announcement item : items) {
            AnnouncementVo vo = new AnnouncementVo();
            BeanUtils.copyProperties(item, vo);
            if (item.getCreated() != null) {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                vo.setCreateDate(dateFormat.format(item.getCreated()));
            }
            announlist.add(vo);
        }
        pageResult.setItems(announlist);
        return pageResult;
    }

    public void addContacts(Map<String, Integer> map) {
        Long longInUserId = UserHolder.getUserId();
        Integer userId = map.get("userId");
        announcementApi.addFriend(longInUserId, userId);
        huanXinTemplate.makeFriends(longInUserId, userId.longValue());
    }

    public PageResult<ContactVo> ContactsList(Long page, Long pagesize, String keyword) {
        Long longInUserId = UserHolder.getUserId();
        PageResult pageResult = friendApi.findPageResult(page,pagesize,longInUserId);
        List<Friend> items = pageResult.getItems();
        List<ContactVo> contactVos=new ArrayList<>();
        if (!CollectionUtils.isEmpty(items)){
            List<Long> friendIdList = items.stream().map(Friend::getFriendId).collect(Collectors.toList());
            List<UserInfo> userInfoList = userInfoApi.findByBatchIds(friendIdList);
            userInfoList.forEach(userInfo -> {
                ContactVo vo = new ContactVo();
                BeanUtils.copyProperties(userInfo,vo);
                vo.setUserId(userInfo.getId().toString());
                contactVos.add(vo);
            });
        }
        pageResult.setItems(contactVos);
        return pageResult;
    }

    public PageResult<MessageVo> pageLoves(Long page, Long pagesize,int type) {
        Long loginUserId = UserHolder.getUserId();
       PageResult pageResult= announcementApi.pageLoves(page,pagesize,loginUserId,type);
        List<Comment> items = pageResult.getItems();
        List<MessageVo> messageVoList=new ArrayList<>();
        if (!CollectionUtils.isEmpty(items)){
            List<Long> userIds = items.stream().map(Comment::getUserId).collect(Collectors.toList());
            List<UserInfo> userInfos = userInfoApi.findByBatchIds(userIds);
            Map<Long, UserInfo> userInfoMap = userInfos.stream().collect(Collectors.toMap(UserInfo::getId, u -> u));
            for (Comment item : items) {
                MessageVo vo = new MessageVo();
                BeanUtils.copyProperties(userInfoMap.get(item.getUserId()),vo);
                vo.setCreateDate(RelativeDateFormat.format(new Date(item.getCreated())));
                vo.setId(item.getId().toHexString());
                messageVoList.add(vo);
            }
        }
        pageResult.setItems(messageVoList);
        return pageResult;
    }
}
